package nobodyCodesThat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Canvas;

public class AnimationLoader {
	private static Singleton instance = Singleton.getInstance();

	private static boolean initialized = false;

	public static void stopAnim() {
		instance.running = false;
		resetAnim();
	}

	private static void resetAnim() {
		instance.animImageNumber = 0;
		Image image = new Image(instance.display, instance.loader.data[0]);
		instance.gc.drawImage(image, instance.loader.data[0].x,
				instance.loader.data[0].y);
		instance.canvasAnimGif.redraw();
	}

	private static void createAnim() {
		instance.loader = new ImageLoader();
		instance.loader.load(Constants.ANIM_GIF);
		instance.canvasAnimGif = new Canvas(instance.groupMainButtons, SWT.NONE);
		instance.canvasAnimGif.setBounds(160, 20, 100, 30);
		final Image image = new Image(instance.display, instance.loader.data[0]);
		instance.gc = new GC(image);
		instance.canvasAnimGif.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				event.gc.drawImage(image, 0, 0);
			}
		});
		instance.shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				instance.running = false;
			}
		});
		initialized = true;
	}

	public static void startAnim() {
		if (!initialized) {
			createAnim();
		}
		instance.running = true;
		AnimationThread threadAnim = new AnimationThread();
		threadAnim.setName("AnimGIF");
		threadAnim.start();
	}
}